package com.googlecode.maurersmarbles;

import java.util.ArrayList;
import java.util.List;

public class GameState {

	/**
	 * The player number for who's turn it is.
	 */
	private int currentPlayersTurn = 0;
	/**
	 * The player that is currently the dealer.
	 */
	protected int dealer = 0;
	/**
	 * The array that contains the player colors. TODO Should we make this
	 * static or move elsewhere?
	 */
	protected final int playerColors[] = new int[GameLogic.NUMBER_OF_PLAYERS];
	/**
	 * The card deck.
	 */
	protected List<Card> cardDeck;
	/**
	 * The array that holds the players.
	 */
	protected final Player[] players;

	/**
	 * Creates a new GameState.
	 */
	public GameState() {

		players = new Player[GameLogic.NUMBER_OF_PLAYERS];
		for (int i = 0; i < GameLogic.NUMBER_OF_PLAYERS; i++) {
			players[i] = new Player(i);
		}

	}

	/**
	 * Creates a new GameState.
	 * @param gameState the game state to copy
	 */
	public GameState(GameState gameState) {

		cardDeck = new ArrayList<Card>();
		players = new Player[GameLogic.NUMBER_OF_PLAYERS];

		if (gameState != null) {

			currentPlayersTurn = gameState.currentPlayersTurn;
			dealer = gameState.dealer;

			for (int i = 0; i < playerColors.length; i++) {
				playerColors[i] = gameState.playerColors[i];
			}

			if (gameState.cardDeck != null) {
				// TODO Do these cards need to be a copy?
				cardDeck.addAll(gameState.cardDeck);
			}

			for (int i = 0; i < players.length; i++) {
				players[i] = new Player(gameState.players[i]);
			}

		}

	}

	/**
	 * Returns the current players turn.
	 * @return the current players turn
	 */
	public int getCurrentPlayersTurn() {
		return currentPlayersTurn;
	}

	/**
	 * Sets the current players turn.
	 * @param currentPlayersTurn the current players turn
	 */
	public void setCurrentPlayersTurn(int currentPlayersTurn) {
		this.currentPlayersTurn = currentPlayersTurn;
	}

	/**
	 * Updates the player colors based on the user's color.
	 * @param colorIndex
	 */
	public void setUserPlayerColor(int colorIndex) {
		for (int i = 0; i < GameLogic.NUMBER_OF_PLAYERS; i++) {
			playerColors[i] = GameLogic.COLORS[colorIndex];
			colorIndex++;
			if (colorIndex >= GameLogic.COLORS.length) {
				colorIndex = 0;
			}
		}
	}

	/**
	 * Returns the players array.
	 * @return the players array
	 */
	public Player[] getPlayers() {
		return players;
	}

}
